package cn.zhouchao.org.controller;

import cn.zhouchao.basic.ann.PreAuthorize;
import cn.zhouchao.basic.util.AjaxResult;
import cn.zhouchao.basic.util.PageList;
import cn.zhouchao.org.domain.Employee;
import cn.zhouchao.org.query.EmployeeQuery;
import cn.zhouchao.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/*
@RestController
组合注解：返回json格式
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
 */

/**
 * restful风格
 * 员工管理
 */
@RestController// 组合注解：返回json格式
@RequestMapping("/emp")
@Api(value = "员工管理系统",description = "员工的CRUD")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     *查询所有对象
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有对象")
//    @PreAuthorize(name = "获取所有员工", sn = "emp:findAll")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 查询单个对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    /**
     * 新增或修改单个对象
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增或修改单个对象")
//    @PreAuthorize(name = "保存员工", sn = "emp:save")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (null==employee.getId()){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail("操作失败！");
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除单个对象
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个对象")
//    @PreAuthorize(name = "删除员工", sn = "emp:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
//    @PreAuthorize(name = "员工分页列表", sn = "emp:queryByPage")
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return employeeService.queryByPage(query);
    }

    /**
     * 批量删除多个对象
     * 携带的时ids集合，作为对象放在RequestBody请求体中
     * 故与删除不同，就不在路径上+参数，而是通过请求体携带对象（多个）
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除多个对象")
    @PreAuthorize(name = "批量删除员工", sn = "emp:batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        employeeService.batchDelete(ids);
        return AjaxResult.me().success();
    }

}
